﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SistemaInventarioMVC.Models.Repository;
using SistemaInventarioMVC.Models.Entidades;

namespace SistemaInventarioMVC.Controllers
{
    public class MarcaController : Controller
    {
        private UnitOfWork unitOfWork = null;

        public MarcaController() 
        {
            unitOfWork = new UnitOfWork();
        }

        public ActionResult Index()
        {
            return View(unitOfWork.Marca.GetAll());
        }

        public ActionResult Detalles(int id)
        {
            return View(unitOfWork.Marca.GetByID(id));
        }

        public ActionResult Crear()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Crear(Marca marca)
        {
            try
            {
                unitOfWork.Marca.Insert(marca);
                unitOfWork.Save();
                return RedirectToAction("Index");
            }
            catch
            {
                ModelState.AddModelError("", "Error del sistema. Intentelo de nuevo, sino contacte su adminsitrador de sistemas.");
                return View();
            }
        }


        public ActionResult Editar(int id)
        {
            return View(unitOfWork.Marca.GetByID(id));
        }

        [HttpPost]
        public ActionResult Editar(int id, Marca marca)
        {
            try
            {
                unitOfWork.Marca.Update(marca);
                unitOfWork.Save();
                return RedirectToAction("Index");
            }
            catch
            {
                ModelState.AddModelError("", "Error del sistema. Intentelo de nuevo, sino contacte su adminsitrador de sistemas.");
                return View();
            }
        }


        public ActionResult Borrar(int id)
        {
            return View(unitOfWork.Marca.GetByID(id));
        }


        [HttpPost]
        public ActionResult Borrar(int id, Marca marca)
        {
            try
            {
                unitOfWork.Marca.Delete(id);
                return RedirectToAction("Index");
            }
            catch
            {
                ModelState.AddModelError("", "Error del sistema. Intentelo de nuevo, sino contacte su adminsitrador de sistemas.");
                return View();
            }
        }
    }
}
